﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Collections.ObjectModel;
using System.ComponentModel;

using System.ComponentModel.Composition;
using FunctionPoint.Common;
using FunctionPoint.Utility;

namespace FunctionPoint
{
    /// <summary>
    /// Interaction logic for FunctionPointExplorerToolWindow.xaml
    /// </summary>
    /// 
    [Export(typeof(FunctionPointExplorerToolWindow))]
    public partial class FunctionPointExplorerToolWindow : UserControl
    {

        public static readonly DependencyProperty QueryWorkItemCollectionProperty =
            DependencyProperty.Register("QueryWorkItemCollection", typeof(ObservableCollection<WorkItem>), typeof(FunctionPointExplorerToolWindow));

        public static readonly DependencyProperty QueryHierarchyProperty =
            DependencyProperty.Register("QueryHierarchy", typeof(ObservableCollection<QueryItemBase>), typeof(FunctionPointExplorerToolWindow));

        public static readonly DependencyProperty SelectedWorkItemsProperty =
            DependencyProperty.Register("SelectedWorkItems", typeof(ObservableCollection<WorkItem>), typeof(FunctionPointExplorerToolWindow));

        [Description("Represents all selected work items by check box in hierarchy")]
        public ObservableCollection<WorkItem> SelectedWorkItems
        {
            get
            {
                return (ObservableCollection<WorkItem>)GetValue(SelectedWorkItemsProperty);
            }
            set
            {
                SetValue(SelectedWorkItemsProperty, value);
            }
        }

       
        public FunctionPointExplorerToolWindow()
        {
            //SystemColors.ControlDarkDarkBrush
            InitializeComponent();

            
            SelectedWorkItems = new ObservableCollection<WorkItem>();
        }


        
        private void btnSelectQuery_Click(object sender, RoutedEventArgs e)
        {
            //popupSelect.IsOpen = true;
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {

        }

     
        private void cmbFunctionPoints_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            
        }

        private void UserControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {

        }

        private void cmbSelectedQuery_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var selectedItem = ((ComboBox)e.OriginalSource).SelectedItem;

            if (selectedItem is WorkItemQuery)
            {
                WorkItemQuery wiQuery = (WorkItemQuery)selectedItem;
                if (!wiQuery.IsLoaded)
                {
                    var utility = TfsUtility.GetDefaultInstance();
                    utility.GetQueryWorkItems(wiQuery);
                }
            }
         
        }

        //private void tlvWorkItemList_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangeEventArgs e)
        //{
        //    e.AddedItems.ToList().ForEach(addedItem =>
        //    {
        //        WorkItem workItem = addedItem as WorkItem;

        //        if (workItem != null)
        //        {
        //            var existingItem = this.SelectedWorkItems.FirstOrDefault(o => o.WorkItemId == workItem.WorkItemId);

        //            if (existingItem == null)
        //                SelectedWorkItems.Add(workItem);
                    
        //        }
        //    });

        //    // unselected

        //    e.RemovedItems.ToList().ForEach(removedItem =>
        //        {
        //            WorkItem workItem = removedItem as WorkItem;
        //            if (workItem != null)
        //            {
        //                var existingItem = this.SelectedWorkItems.FirstOrDefault(o => o.WorkItemId == workItem.WorkItemId);

        //                if (existingItem != null)
        //                    SelectedWorkItems.Remove(existingItem);
        //            }
        //        });

        //}
    }
}
